package com.pt.pok.proxy.client.integration.channel;

import com.pt.pok.GeneralProxyException;
import com.pt.pok.ProtocolConverter;
import com.pt.pok.model.PokerCommand;

import java.nio.ByteBuffer;

import static com.pt.pok.proxy.client.integration.util.zip.CompressionUtil.*;
import static com.pt.pok.proxy.client.integration.util.net.BinaryFormatUtility.*;

/**
 * @author Roman.Ivanov@playtech.com
 *         <p/>
 *         FAKE IMPLEMENTER
 */
public class BinaryClientSessionProxyDelegate {

    private final ProtocolConverter<byte[]> protobufConverter;
    private final ProtocolConverter<String> stringProtocolConverter;

    public BinaryClientSessionProxyDelegate(final ProtocolConverter<byte[]> protobufConverter, final ProtocolConverter<String> stringProtocolConverter) {
        this.protobufConverter = protobufConverter;
        this.stringProtocolConverter = stringProtocolConverter;
    }

    public void processCmd(ByteBuffer networkMessage)
        throws GeneralProxyException {

        final byte[] data = isCompressedData(networkMessage) ? decompress(getRawData(networkMessage)) : getRawData(networkMessage);

        final PokerCommand pokerCommand = protobufConverter.convertFromProtocolCommand(getClassId(networkMessage), data);

        final String stringCommand = stringProtocolConverter.convertToProtocolCommand(pokerCommand);

        onStringEvent(stringCommand);
    }

    public ByteBuffer convertStringToByteBuffer(int classId, String command)
        throws GeneralProxyException {

        final PokerCommand domainCommand = stringProtocolConverter.convertFromProtocolCommand(classId, command);

        byte[] data = protobufConverter.convertToProtocolCommand(domainCommand);
        boolean needCompression = checkIfCompressionRequired(data);
        if (needCompression) {
            data = compress(data);
        }

        return buildNetworkMessage(false, needCompression, classId, data);
    }

    /**
     * This is the method mentioned as -
     * "call the sendMessage method and pass the byte buffer (no length) as a parameter"
     * 
     * @param command binary protocol command
     */
    public void sendMessage(ByteBuffer command) {

    }

    /**
     * This is the method mentioned as - "call the ***** method and pass the generated string as a parameter"
     * 
     * @param command string protocol command
     */
    public void onStringEvent(String command) {

    }

}
